var CASE_UID;
var CASE_CODE;

var FileTaskCtl = function() {
	this.fileTaskList = [];
	this.addTask = function(taskItem) {
		this.fileTaskList.push(taskItem);
	}
	this.removeFileTask = function(containnerId, isRemoveDom) {
		var removeIndex = -1;
		for(var i = 0; i < this.fileTaskList.length; i++) {
			var taskItem = this.fileTaskList[i];

			if(taskItem.container == containnerId) {
				removeIndex = i;
				break;
			}
		}
		if(removeIndex >= 0) {
			this.fileTaskList.splice(removeIndex, 1);
			if(isRemoveDom) {
				$("#" + containnerId).remove();
			}

		}
	}

	this.startAll = function() {
		console.log("----" +this.fileTaskList.length);
		for(var index = 0; index <= this.fileTaskList.length-1; index++) {

			var taskItem = this.fileTaskList[index];
			taskItem.start();
			console.log(index);
		}
	}
	this.isAllFinished = function() {
		for(var i = 0; i < this.fileTaskList.length; i++) {

			var taskItem = this.fileTaskList[i];
			if(!taskItem.finished) {
				return false;
			}

		}
		return true;
	}
	this.getTaskCount = function(){
		return this.fileTaskList.length;
	}
}

var filetask = function(container, file, fileExtraOpt) {
	var _that = this;
	this.container = container;
	this.file = file;
	this.fileExtraOpt = fileExtraOpt;

	this.setTask = function(taskId) {
		this.taskId = taskId;
	}
	this.finished = false;
	this.sucess   = false;
	this.task = plus.uploader.createUpload(app.SERVER_URL + '/caseApi/uploadImage', {
			method: "POST"
		},
		function(t, status) { //上传完成  
			if(status == 200) {

			} else {
				$("#" + _that.container + " .delbtn").show();
				console.log("上传失败：" + status);
			}
		}
	);

	this.onStateChanged = function(upload, status) {

		var progressVal = (upload.uploadedSize / upload.totalSize) * 100;
		mui('#' + _that.container + "_progress").progressbar().setProgress(progressVal);
		//					console.log("tasiid:" + upload.__UUID__ +"send:" + upload.uploadedSize + "     status:" + status + "        total : " + upload.totalSize);
		if(upload.state == 4 && status == 200) {
			_that.finished = true;
			if(fileTaskController.isAllFinished()) {
					var upPageView = plus.webview.getWebviewById('/html/case/upimage.html');
					mui.fire(upPageView,'allFinish',{});
					
			}
			var result = jQuery.parseJSON(upload.responseText);
			console.log(upload.responseText);
			if(result.result_code && result.result_code == "0") {
			
				fileTaskController.removeFileTask(_that.container, false);
				
			} else {

				$("#" + _that.container + " .delbtn").show();

				console.log("task finished");
				mui('#' + _that.container + "_progress").progressbar().setProgress(0);

			}

		}

	}
	this.taskId = this.task.__UUID__;
	this.start = function() {
		$("#" + this.container + " .delbtn").hide();
		this.task.addFile(this.file, {
			key: this.container
		});
		if(this.fileExtraOpt) {
			
			for(var name in this.fileExtraOpt) {
				console.log(name + ":" +this.fileExtraOpt[name]);
				this.task.addData(name, this.fileExtraOpt[name]);
			}
		}
		console.log(typeof this.fileExtraOpt["LAST_MODIFY"]);
		this.task.addEventListener("statechanged", this.onStateChanged, false);
		this.task.start();
	}

}




function getUid() {  
    return Math.floor(Math.random() * 100000000 + 10000000).toString();  
}

function getPreviewHtml (itemId,file){
//	resizeImage(file,getUid(),_img_);
	
//	var li = $("<li class='pickimg' id='"+ itemId+"'>");
//	
//	var delBtn = $("<div class='delbtn'><span class='mui-icon mui-icon-closeempty deltxt'></span></div>");
//	
//	var progress = $("<div class='mui-progressbar-wrapper'> "+ 
//						   "<p class='mui-progressbar mui-progressbar-royal' id='" +itemId+ "_progress'" + "data-progress='0'><span></span></p> " + 
//					"</div>");		
//	li.append(delBtn);
//	li.append(_img_);
//	li.append(progress);
	var li = $("<li class='pickimg' id='"+ itemId+"'></li>");
    var ulWidth = $(".photo-list").width();
    var liWidth = ulWidth/4 ;
    li.height(liWidth);
	console.log($(".photo-list").width());
	var htmlContent = 
						"<div class='delbtn'><span class='mui-icon mui-icon-closeempty deltxt'></span></div>"+
						"<img src='../../images/60x60.gif' /> " + 
						"<div class='mui-progressbar-wrapper'> "+ 
						   "<p class='mui-progressbar mui-progressbar-royal' id='" +itemId+ "_progress'" + "data-progress='0'><span></span></p> " + 
						"</div>" ;
	li.html(htmlContent);				   
//	console.log(li.prop("outerHTML"));				  
	return li.prop("outerHTML");
}
   
function resizeImage(src,elId,success) {
		var destName = '_doc/' + elId + ".jpg";

		plus.zip.compressImage(
			{
				src: src,
				dst: destName,
				overwrite: true,
				format: 'jpg',
				quality: 100 //图片质量不再修改，以免失真
			},
			function(e) {
				console.log(JSON.stringify(e));
//				imageObj.src = e.target;
				
				success(e.target,elId,src);
				
//							uploadImg(e.target); //上传图片, e.target存的是本地路径！
			},
			function(err) {
					plus.nativeUI.alert('未知错误！',function() {
//								mui.back();
				});
			}
		);
}